package com.warm.spring.exception;

//import com.warm.framework.controller.ExceptionController;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Date: 2020/3/31 17:16
 * @Author: LChuang
 * @Description: 全局异常捕获
 */
//@ControllerAdvice(assignableTypes = {ExceptionController.class})
//@ResponseBody
public class GlobalExceptionHandler {

    /**
     *  也可以将 BaseException 换为 RuntimeException
     *  因为 RuntimeException 是 BaseException 的父类
     *
     *  拦截所有异常 @ExceptionHandler(BaseException.class)
     *  拦截BaseException异常 @ExceptionHandler(BaseException.class)
     */
    @ExceptionHandler(BaseException.class)
    public ResponseEntity<?> handleAppException(BaseException ex, HttpServletRequest request) {
        ErrorResp representation = new ErrorResp(ex, request.getRequestURI());
        return new ResponseEntity<>(representation, new HttpHeaders(), ex.getError().getStatus());
    }
}
